Popular Searches
Popular Course Categories
Popular Courses

Understanding Pandas Series and DataFrames

Understanding Pandas Series and DataFrames

Python Data Analysis and Visualization

Understanding Pandas Series and DataFrames

Course: Python Training

Topic: Understanding Pandas Series and DataFrames

Course Link: Python Training Course – JustAcademy

Demo Link: Register for a Free Course Demo

Website: JustAcademy Official Website


1. Introduction to Pandas

Pandas is a popular open-source Python library used for data manipulation, data analysis, and data preparation. It provides convenient data structures and functions for working with structured data, including tables, CSV files, Excel files, and database query results.

Two of the most important Pandas data structures are Series and DataFrame. A Series represents one-dimensional labeled data, while a DataFrame represents two-dimensional data organized into rows and columns.

Pandas is commonly used in data analytics, machine learning preparation, business intelligence, financial analysis, reporting, and exploratory data analysis.

Why Use Pandas?

  • Read and write data in formats such as CSV, Excel, and JSON.
  • Organize data into labeled rows and columns.
  • Select, filter, sort, and transform datasets.
  • Identify and handle missing values.
  • Calculate statistics and summarize information.
  • Combine datasets and prepare data for visualization or machine learning.

2. Installing and Importing Pandas

Before using Pandas, install it in your Python environment if it is not already available.

pip install pandas

Import Pandas using the conventional alias pd:

import pandas as pd

print(pd.__version__)

The alias pd is widely used in Python projects and examples, making Pandas functions easier to write.

3. Understanding Pandas Data Structures

Pandas primarily provides two data structures for handling tabular information.

Feature Series DataFrame
Dimensions One-dimensional Two-dimensional
Structure One labeled sequence of values Rows and named columns
Labels Index labels Row index and column labels
Typical use Representing one variable Representing a complete dataset
Example Product prices Product names, prices, and quantities

4. What Is a Pandas Series?

A Pandas Series is a one-dimensional labeled array that can store values such as integers, floating-point numbers, strings, Boolean values, dates, or Python objects. Each value is associated with an index label.

By default, Pandas assigns integer index labels beginning at zero. You can also specify custom index labels to make the data easier to identify.

4.1 Creating a Series from a List

import pandas as pd

marks = pd.Series([85, 90, 78, 92])

print(marks)

Output:

0    85
1    90
2    78
3    92
dtype: int64

In this example, the left side contains index labels, and the right side contains values. The final line identifies the data type used by the Series.

4.2 Creating a Series with Custom Index Labels

import pandas as pd

marks = pd.Series(
    [85, 90, 78],
    index=["Amit", "Neha", "Rahul"]
)

print(marks)

Output:

Amit     85
Neha     90
Rahul    78
dtype: int64

Custom labels make it possible to identify values by meaningful names instead of relying only on numerical positions.

4.3 Creating a Series from a Dictionary

import pandas as pd

sales = {
    "January": 12000,
    "February": 15000,
    "March": 18000
}

monthly_sales = pd.Series(sales)

print(monthly_sales)

When a dictionary is passed to pd.Series(), its keys normally become index labels and its values become Series data.

4.4 Creating a Series with a Single Value

import pandas as pd

temperatures = pd.Series(25, index=["Monday", "Tuesday", "Wednesday"])

print(temperatures)

Output:

Monday       25
Tuesday      25
Wednesday    25
dtype: int64

A scalar value is repeated for each supplied index label.

5. Accessing Series Values

Series values can be selected by label or by position. Understanding the difference helps avoid indexing errors.

5.1 Accessing Values by Label with loc

import pandas as pd

marks = pd.Series(
    [85, 90, 78],
    index=["Amit", "Neha", "Rahul"]
)

print(marks.loc["Neha"])

Output:

90

The loc accessor selects data using index labels.

5.2 Accessing Values by Position with iloc

print(marks.iloc[0])
print(marks.iloc[2])

Output:

85
78

The iloc accessor selects data by integer position, starting at zero.

5.3 Selecting Multiple Values

print(marks.loc[["Amit", "Rahul"]])
print(marks.iloc[[0, 2]])

Both statements select two values, but the first uses labels and the second uses positions.

6. Series Attributes and Methods

Series attributes provide information about the data, while methods perform operations on it.

Attribute or Method Purpose Example
index Returns index labels marks.index
values Returns underlying values as an array-like object marks.values
dtype Shows the Series data type marks.dtype
shape Returns the Series dimensions marks.shape
size Returns the number of elements marks.size
head() Shows the first few values marks.head()
tail() Shows the last few values marks.tail()
describe() Summarizes numeric data marks.describe()
mean() Calculates the mean marks.mean()
sum() Calculates the total marks.sum()

Example

import pandas as pd

marks = pd.Series([85, 90, 78, 92, 88])

print("Mean:", marks.mean())
print("Total:", marks.sum())
print("Maximum:", marks.max())
print("Minimum:", marks.min())
print("Number of values:", marks.size)
print("Data type:", marks.dtype)
print(marks.describe())

7. Performing Calculations on a Series

Pandas supports vectorized arithmetic, which allows calculations to be applied to an entire Series without writing a loop for each value.

import pandas as pd

prices = pd.Series([100, 200, 300])

print(prices + 10)
print(prices * 2)
print(prices / 10)
print(prices ** 2)

These operations are applied element by element. For example, adding 10 increases every price by 10.

7.1 Filtering a Series

import pandas as pd

marks = pd.Series([45, 78, 92, 33, 85])

passed = marks[marks >= 40]

print(passed)

Boolean filtering returns the values whose conditions evaluate to True, retaining their original index labels.

8. What Is a Pandas DataFrame?

A Pandas DataFrame is a two-dimensional labeled data structure consisting of rows and columns. Each column is a Series, and different columns can store different data types.

For example, a student dataset might contain a name column, an age column, and a marks column. These columns together form a DataFrame.

8.1 Creating a DataFrame from a Dictionary

import pandas as pd

data = {
    "Name": ["Amit", "Neha", "Rahul"],
    "Age": [21, 22, 20],
    "Marks": [85, 90, 78]
}

df = pd.DataFrame(data)

print(df)

Output:

    Name  Age  Marks
0   Amit   21     85
1   Neha   22     90
2  Rahul   20     78

Dictionary keys become column names, while the corresponding lists provide the values for each row. Pandas automatically assigns a default integer index.

8.2 Creating a DataFrame from a List of Dictionaries

import pandas as pd

students = [
    {"Name": "Amit", "Marks": 85},
    {"Name": "Neha", "Marks": 90},
    {"Name": "Rahul", "Marks": 78}
]

df = pd.DataFrame(students)

print(df)

Each dictionary represents a row. If a dictionary does not contain a column present in another dictionary, the missing value is generally represented as NaN or an appropriate missing-value marker.

8.3 Creating a DataFrame from a List of Lists

import pandas as pd

data = [
    ["Amit", 21, 85],
    ["Neha", 22, 90],
    ["Rahul", 20, 78]
]

df = pd.DataFrame(
    data,
    columns=["Name", "Age", "Marks"]
)

print(df)

When using a list of lists, specify the column names if you want meaningful labels instead of automatically assigned numeric column names.

8.4 Creating an Empty DataFrame

import pandas as pd

df = pd.DataFrame(columns=["Name", "Age", "Marks"])

print(df)

An empty DataFrame can serve as a starting structure, although repeatedly adding individual rows to a DataFrame is often inefficient for large workloads.

9. Understanding DataFrame Rows, Columns, and Indexes

A DataFrame has three important structural elements:

  • Rows: Individual records, such as one student's details.
  • Columns: Variables or fields, such as Name, Age, and Marks.
  • Index: Labels used to identify rows. The default index is a sequence of integers.

Consider this example:

import pandas as pd

df = pd.DataFrame({
    "Product": ["Laptop", "Phone", "Tablet"],
    "Price": [60000, 25000, 18000],
    "Stock": [10, 25, 15]
})

print(df)

Each row describes a product, and each column describes one property of those products.

10. Important DataFrame Attributes

Attribute Description Example
df.shape Returns row and column counts (3, 3)
df.columns Returns column labels Index([...])
df.index Returns row labels RangeIndex(...)
df.dtypes Shows the data type of each column Series of data types
df.size Returns total number of cells 9 for a 3 × 3 DataFrame
df.ndim Returns the number of dimensions 2
df.empty Checks whether either axis has length zero True or False

Example

import pandas as pd

df = pd.DataFrame({
    "Name": ["Amit", "Neha", "Rahul"],
    "Age": [21, 22, 20],
    "Marks": [85, 90, 78]
})

print("Shape:", df.shape)
print("Columns:", df.columns)
print("Index:", df.index)
print("Data types:")
print(df.dtypes)
print("Dimensions:", df.ndim)
print("Total cells:", df.size)

11. Viewing and Inspecting a DataFrame

Before analyzing a dataset, inspect its structure, sample records, and data types. This helps reveal unexpected values, missing columns, and possible data-quality problems.

import pandas as pd

df = pd.DataFrame({
    "Name": ["Amit", "Neha", "Rahul", "Priya"],
    "Age": [21, 22, 20, 23],
    "Marks": [85, 90, 78, 95]
})

print(df.head())
print(df.tail())
print(df.info())
print(df.describe())
print(df.sample(2))
  • head() displays the first five rows by default.
  • tail() displays the last five rows by default.
  • info() summarizes column names, non-null counts, and data types.
  • describe() provides summary statistics for numeric columns by default.
  • sample(2) selects two randomly sampled rows when enough rows are available.

12. Selecting a Single Column

Use the column name inside square brackets to select a single column as a Series.

import pandas as pd

df = pd.DataFrame({
    "Name": ["Amit", "Neha", "Rahul"],
    "Age": [21, 22, 20],
    "Marks": [85, 90, 78]
})

names = df["Name"]

print(names)
print(type(names))

The expression df["Name"] returns a Series rather than a DataFrame.

12.1 Using Dot Notation

ages = df.Age
print(ages)

Dot notation can be convenient, but bracket notation is more reliable. Use df["column_name"] when column names contain spaces, conflict with DataFrame attributes, or are stored in variables.

13. Selecting Multiple Columns

Pass a list of column names inside the outer brackets to select multiple columns. The result is a DataFrame.

selected = df[["Name", "Marks"]]

print(selected)

This is useful when a dataset contains many columns but only a few are required for an analysis.

14. Selecting Rows with loc and iloc

14.1 Using loc

The loc accessor selects rows or cells by their index labels and can also use Boolean conditions.

import pandas as pd

df = pd.DataFrame({
    "Name": ["Amit", "Neha", "Rahul"],
    "Marks": [85, 90, 78]
}, index=["S1", "S2", "S3"])

print(df.loc["S2"])
print(df.loc["S1":"S2"])
print(df.loc["S2", "Marks"])

When slicing with label-based loc, both endpoints are generally included when they exist in the index.

14.2 Using iloc

print(df.iloc[0])
print(df.iloc[0:2])
print(df.iloc[1, 1])

The iloc accessor uses integer positions. In ordinary Python-style slices such as 0:2, the ending position is excluded.

14.3 loc Versus iloc

Feature loc iloc
Selection basis Labels Integer positions
Example df.loc["S1"] df.iloc[0]
Slice endpoint Usually included Excluded
Useful when Index labels have meaning Exact row or column positions are known

15. Filtering DataFrame Rows

Filtering allows you to select records that meet specified conditions.

15.1 Filtering with One Condition

import pandas as pd

df = pd.DataFrame({
    "Name": ["Amit", "Neha", "Rahul", "Priya"],
    "Marks": [85, 90, 35, 95],
    "Age": [21, 22, 20, 23]
})

passed = df[df["Marks"] >= 40]

print(passed)

This example selects students with marks greater than or equal to 40.

15.2 Filtering with Multiple Conditions

high_scorers = df[
    (df["Marks"] >= 80) & (df["Age"] >= 21)
]

print(high_scorers)

Use & for AND, | for OR, and ~ for NOT in Pandas Boolean expressions. Put each comparison in parentheses to avoid operator-precedence errors.

15.3 Using isin()

selected_students = df[df["Name"].isin(["Amit", "Priya"])]

print(selected_students)

The isin() method checks whether values belong to a specified collection.

15.4 Filtering Text

df = pd.DataFrame({
    "Product": ["Laptop", "Phone", "Laptop Stand", "Tablet"]
})

laptop_items = df[
    df["Product"].str.contains("Laptop", case=False, na=False)
]

print(laptop_items)

String methods such as str.contains(), str.startswith(), and str.endswith() are useful for text-based filtering. The na=False argument treats missing text values as non-matches.

16. Adding and Modifying DataFrame Columns

Columns can be created from calculations, constants, lists, or existing data.

16.1 Adding a New Column

import pandas as pd

df = pd.DataFrame({
    "Product": ["Laptop", "Phone", "Tablet"],
    "Price": [60000, 25000, 18000]
})

df["Tax"] = df["Price"] * 0.18

print(df)

16.2 Creating a Calculated Column

df["Discounted_Price"] = df["Price"] * 0.90

print(df)

This example calculates a price after a 10% discount. In real applications, confirm whether prices include tax and whether discounts are applied before or after tax.

16.3 Updating Existing Values

df.loc[df["Product"] == "Phone", "Price"] = 27000

print(df)

Use loc to update rows that satisfy a condition. This makes the intended update explicit.

16.4 Renaming Columns

df = df.rename(columns={
    "Product": "Product_Name",
    "Price": "Original_Price"
})

print(df)

By default, rename() returns a modified DataFrame without changing the original object unless the result is assigned back or an appropriate in-place option is used.

17. Removing Columns and Rows

The drop() method removes specified rows or columns.

import pandas as pd

df = pd.DataFrame({
    "Name": ["Amit", "Neha", "Rahul"],
    "Age": [21, 22, 20],
    "Marks": [85, 90, 78]
})

df_without_age = df.drop(columns=["Age"])

print(df_without_age)

To remove a row by its index label:

df_without_first = df.drop(index=0)

print(df_without_first)

By default, these examples return new DataFrames. Assign the result if you want to retain the change in a variable.

18. Sorting Series and DataFrames

Sorting organizes data according to values or index labels.

18.1 Sorting a Series

import pandas as pd

marks = pd.Series([85, 90, 78, 95])

print(marks.sort_values())
print(marks.sort_values(ascending=False))

18.2 Sorting a DataFrame by a Column

df = pd.DataFrame({
    "Name": ["Amit", "Neha", "Rahul"],
    "Marks": [85, 90, 78]
})

print(df.sort_values(by="Marks"))
print(df.sort_values(by="Marks", ascending=False))

18.3 Sorting by Multiple Columns

df = pd.DataFrame({
    "Department": ["Sales", "IT", "Sales", "IT"],
    "Salary": [50000, 70000, 60000, 65000]
})

sorted_df = df.sort_values(
    by=["Department", "Salary"],
    ascending=[True, False]
)

print(sorted_df)

Multiple-column sorting is useful when records must be grouped by one field and ranked by another.

19. Handling Missing Values

Real-world datasets often contain missing information. Pandas provides methods to detect, remove, and replace missing values.

19.1 Detecting Missing Values

import pandas as pd
import numpy as np

df = pd.DataFrame({
    "Name": ["Amit", "Neha", "Rahul"],
    "Marks": [85, np.nan, 78]
})

print(df.isna())
print(df.isna().sum())

isna() identifies missing values, while isna().sum() counts missing values in each column.

19.2 Removing Missing Values

clean_df = df.dropna()

print(clean_df)

dropna() removes rows containing missing values by default. Consider the amount and importance of missing data before removing records.

19.3 Filling Missing Values

df["Marks"] = df["Marks"].fillna(df["Marks"].mean())

print(df)

This replaces missing marks with the column mean. Depending on the data, median, a business-defined default, forward filling, or another method may be more appropriate. Do not replace missing values blindly because this can distort analysis.

20. Understanding Data Types

Data types influence how Pandas stores, compares, and calculates values.

Data Type Typical Use Example
int64 Whole numbers 25
float64 Decimal numbers 98.5
object Often text or mixed Python objects "Amit"
bool True or false values True
datetime64 Date and time values 2026-10-22
category Repeated categorical values "Sales"

20.1 Checking Data Types

print(df.dtypes)

20.2 Converting Data Types

df["Marks"] = df["Marks"].astype("float64")

print(df.dtypes)

Use astype() when the values can be converted safely to the requested type. If the column contains invalid strings, conversion may raise an error.

20.3 Converting Text to Numeric Values

df = pd.DataFrame({
    "Price": ["100", "200", "unknown"]
})

df["Price"] = pd.to_numeric(df["Price"], errors="coerce")

print(df)

The errors="coerce" option converts invalid numeric entries to missing values. Review the resulting missing values rather than silently ignoring them.

21. Applying Functions to Series and DataFrames

Pandas supports applying functions to data, but built-in vectorized operations are usually preferable for simple arithmetic and comparisons.

21.1 Using apply() with a Series

import pandas as pd

marks = pd.Series([35, 65, 85, 95])

def grade_mark(mark):
    if mark >= 90:
        return "A"
    elif mark >= 75:
        return "B"
    elif mark >= 40:
        return "C"
    return "Fail"

grades = marks.apply(grade_mark)

print(grades)

21.2 Using apply() with a DataFrame

df = pd.DataFrame({
    "Math": [80, 90, 70],
    "Science": [85, 88, 75]
})

df["Total"] = df[["Math", "Science"]].sum(axis=1)

print(df)

The parameter axis=1 means the calculation is performed across columns for each row. With axis=0, operations generally work down each column.

22. Vectorized Operations

Vectorization means applying an operation to an entire Series or array instead of explicitly looping over each element. It often produces clearer and faster code.

import pandas as pd

df = pd.DataFrame({
    "Quantity": [2, 3, 5],
    "Unit_Price": [100, 200, 50]
})

df["Total"] = df["Quantity"] * df["Unit_Price"]

print(df)

Here, Pandas calculates the total for each row by multiplying corresponding column values.

For ordinary column calculations, prefer expressions such as df["A"] + df["B"] over a manual Python loop. For complex logic, choose a clear and maintainable approach and measure performance when necessary.

23. Understanding Index Alignment

Pandas aligns Series by their index labels during many operations. This differs from simply matching values by position.

import pandas as pd

first = pd.Series([10, 20], index=["A", "B"])
second = pd.Series([1, 2], index=["B", "C"])

result = first + second

print(result)

Output:

A     NaN
B    21.0
C     NaN
dtype: float64

The values at label B are added because the labels match. Labels A and C do not have matching values in both Series, so the results are missing. This behavior is useful for combining labeled data but can produce unexpected missing values if indexes are not aligned as intended.

24. Reading Data from CSV Files

CSV files are commonly used to store tabular data. Pandas can load a CSV file into a DataFrame with read_csv().

import pandas as pd

df = pd.read_csv("students.csv")

print(df.head())
print(df.info())

If a CSV file contains columns such as Name, Age, and Marks, Pandas usually detects the first row as the column header.

24.1 Reading Specific Columns

df = pd.read_csv(
    "students.csv",
    usecols=["Name", "Marks"]
)

print(df)

Loading only required columns can reduce memory use and simplify processing for wide datasets.

24.2 Handling Missing Markers in CSV Files

df = pd.read_csv(
    "students.csv",
    na_values=["NA", "N/A", "missing"]
)

print(df.isna().sum())

The na_values parameter tells Pandas which additional text values should be interpreted as missing data.

25. Writing Data to CSV Files

The to_csv() method saves a DataFrame to a CSV file.

import pandas as pd

df = pd.DataFrame({
    "Name": ["Amit", "Neha", "Rahul"],
    "Marks": [85, 90, 78]
})

df.to_csv("students_output.csv", index=False)

The argument index=False prevents the DataFrame index from being written as an additional column. If the index is meaningful and needs to be preserved, omit this argument or set it to True.

26. Reading and Writing Excel Files

Pandas supports Excel files through methods such as read_excel() and to_excel(). Depending on the file format, an appropriate Excel engine may need to be installed.

import pandas as pd

df = pd.read_excel("students.xlsx", sheet_name="Sheet1")

print(df.head())

df.to_excel("students_output.xlsx", index=False)

For workbooks with multiple sheets, use the sheet_name argument to specify the required sheet. Confirm the sheet names and file paths before processing.

27. Combining DataFrames

Pandas provides several ways to combine datasets. The correct method depends on whether records should be stacked or matched by a key.

27.1 Concatenating DataFrames

import pandas as pd

first = pd.DataFrame({
    "Name": ["Amit", "Neha"],
    "Marks": [85, 90]
})

second = pd.DataFrame({
    "Name": ["Rahul", "Priya"],
    "Marks": [78, 95]
})

combined = pd.concat([first, second], ignore_index=True)

print(combined)

concat() stacks the rows in this example. The ignore_index=True argument creates a new sequential index for the combined result.

27.2 Merging DataFrames

import pandas as pd

students = pd.DataFrame({
    "Student_ID": [1, 2, 3],
    "Name": ["Amit", "Neha", "Rahul"]
})

results = pd.DataFrame({
    "Student_ID": [1, 2, 3],
    "Marks": [85, 90, 78]
})

merged = pd.merge(
    students,
    results,
    on="Student_ID",
    how="inner"
)

print(merged)

merge() combines datasets using matching key columns. Common join types include inner, left, right, and outer. Select the join type based on which records must be retained.

28. Grouping and Aggregating Data

The groupby() method groups records by one or more columns so that statistics can be calculated for each group.

import pandas as pd

df = pd.DataFrame({
    "Department": ["IT", "Sales", "IT", "Sales", "HR"],
    "Salary": [70000, 50000, 80000, 60000, 45000]
})

summary = df.groupby("Department")["Salary"].mean()

print(summary)

This example calculates the average salary for each department.

28.1 Multiple Aggregations

summary = df.groupby("Department").agg(
    Average_Salary=("Salary", "mean"),
    Maximum_Salary=("Salary", "max"),
    Employee_Count=("Salary", "count")
)

print(summary)

Named aggregation creates clearly labeled summary columns and is useful for business reports and exploratory analysis.

29. Resetting and Setting Indexes

An index identifies rows, and you can replace it or return it to a regular column when needed.

29.1 Setting an Index

df = pd.DataFrame({
    "Student_ID": [101, 102, 103],
    "Name": ["Amit", "Neha", "Rahul"],
    "Marks": [85, 90, 78]
})

df = df.set_index("Student_ID")

print(df)

29.2 Resetting an Index

df = df.reset_index()

print(df)

set_index() uses one or more columns as the index, while reset_index() restores a default index and normally returns the previous index to a column.

30. Series Versus DataFrame: Practical Differences

Operation Series Example DataFrame Example
Create pd.Series([10, 20]) pd.DataFrame({"A": [10, 20]})
Inspect shape series.shape df.shape
Select one value series.iloc[0] df.iloc[0, 0]
Calculate average series.mean() df["A"].mean()
Filter series[series > 10] df[df["A"] > 10]
Missing values series.isna() df.isna()

31. Common Errors and Troubleshooting

31.1 KeyError When Selecting a Column

Problem: A column selection raises a KeyError.

print(df["Marks"])

Possible cause: The column name is misspelled, has different capitalization, or contains leading or trailing spaces.

Solution: Inspect the column labels and clean them when appropriate.

print(df.columns)

df.columns = df.columns.str.strip()

31.2 IndexError During Positional Selection

Problem: Accessing a position that does not exist raises an indexing error.

Solution: Check the number of available rows or columns before selecting a position.

print(df.shape)

if not df.empty:
    print(df.iloc[0])

31.3 TypeError During Calculations

Problem: A calculation fails because a column contains strings or other unexpected values.

Solution: Inspect the column's data type and convert values when appropriate.

print(df.dtypes)

df["Marks"] = pd.to_numeric(df["Marks"], errors="coerce")

31.4 Unexpected Missing Values After Arithmetic

Problem: Adding two Series produces missing values.

Cause: Pandas aligns values by index labels, and some labels may not match.

Solution: Inspect the indexes and determine whether the data should be aligned by labels or deliberately combined by position. Do not reset indexes without confirming the intended relationship between records.

31.5 Incorrect Boolean Filtering

Problem: A condition involving multiple comparisons raises an error.

Solution: Put each comparison in parentheses and use Pandas-compatible logical operators.

filtered = df[
    (df["Age"] >= 18) & (df["Marks"] >= 40)
]

32. Best Practices for Working with Pandas

  • Import Pandas using the conventional alias pd.
  • Inspect datasets with head(), info(), shape, and dtypes before analyzing them.
  • Use meaningful column names and consistent data types.
  • Use loc for label-based selection and conditional updates, and iloc for positional selection.
  • Prefer vectorized operations over unnecessary Python loops.
  • Check for missing values before calculating statistics or building reports.
  • Use merge() for key-based joins and concat() for stacking compatible datasets.
  • Be explicit about whether the index should be saved when exporting to CSV or Excel.
  • Validate data before changing types, filling missing values, or removing records.
  • For large datasets, load only required columns and consider memory usage.
  • Avoid chained assignment when modifying data; use a clear assignment with loc where possible.
  • Keep the original data unchanged when necessary by working on a deliberate copy.

33. Practical Exercises

Exercise 1: Create a Series

Create a Series containing five city names or temperatures. Assign meaningful index labels and print the Series, index, and data type.

Exercise 2: Analyze Student Marks

Create a DataFrame with student names, ages, and marks. Display the first three rows, calculate the average marks, and select students who scored at least 75.

Exercise 3: Update Product Prices

Create a DataFrame containing product names and prices. Add an 18% tax column, calculate a discounted price, and sort the products by their original prices.

Exercise 4: Handle Missing Data

Create a DataFrame with missing values in at least two columns. Count the missing values, fill an appropriate numeric column with its median, and explain why you selected that method.

Exercise 5: Read and Export a CSV

Load a CSV file using read_csv(), inspect its data types, filter a subset of records, and export the result with to_csv().

34. Mini Project: Student Performance Analysis

Project objective: Use Pandas Series and DataFrames to analyze student performance and produce a simple summary report.

Project Requirements

  • Create a dataset containing student IDs, names, mathematics marks, science marks, and English marks.
  • Convert the dataset into a DataFrame.
  • Inspect the data structure and verify the data types.
  • Calculate the total and average marks for each student.
  • Filter students whose average marks are at least 75.
  • Sort students by average marks in descending order.
  • Check for missing values and decide how to handle them.
  • Export the final result to a CSV file.

Sample Solution

import pandas as pd

data = {
    "Student_ID": [101, 102, 103, 104],
    "Name": ["Amit", "Neha", "Rahul", "Priya"],
    "Math": [85, 90, 65, 95],
    "Science": [80, 92, 70, 88],
    "English": [78, 85, 72, 94]
}

df = pd.DataFrame(data)

df["Total"] = df[["Math", "Science", "English"]].sum(axis=1)
df["Average"] = df[["Math", "Science", "English"]].mean(axis=1)

top_students = df[df["Average"] >= 75]

top_students = top_students.sort_values(
    by="Average",
    ascending=False
)

print("Complete Student Report:")
print(df)

print("Students with Average Marks of at Least 75:")
print(top_students)

df.to_csv("student_performance_report.csv", index=False)

Expected Learning Outcomes

  • Understand how to build a DataFrame from structured data.
  • Calculate values across multiple columns.
  • Filter and sort records using conditions.
  • Inspect and export datasets.
  • Apply basic data analysis techniques to a practical scenario.

35. Interview Questions and Answers

Q1. What is Pandas?

Answer: Pandas is an open-source Python library used for data manipulation, analysis, cleaning, and preparation. Its primary data structures include Series and DataFrame.

Q2. What is a Pandas Series?

Answer: A Series is a one-dimensional labeled data structure containing values associated with index labels.

Q3. What is a DataFrame?

Answer: A DataFrame is a two-dimensional labeled structure containing rows and columns. Each column can have its own data type.

Q4. What is the difference between a Series and a DataFrame?

Answer: A Series represents one-dimensional data, whereas a DataFrame represents two-dimensional tabular data with row and column labels.

Q5. How do you create a Series?

Answer: Use pd.Series(), for example, pd.Series([10, 20, 30]).

Q6. How do you create a DataFrame?

Answer: Use pd.DataFrame(), for example, pd.DataFrame({"Name": ["Amit"], "Age": [21]}).

Q7. What is the difference between loc and iloc?

Answer: loc selects data by labels, while iloc selects data by integer positions.

Q8. How do you select a single column?

Answer: Use df["column_name"]. This normally returns a Series.

Q9. How do you select multiple columns?

Answer: Use a list of column names, such as df[["Name", "Age"]]. This returns a DataFrame.

Q10. How do you filter rows based on a condition?

Answer: Use Boolean indexing, such as df[df["Marks"] >= 75].

Q11. How do you identify missing values?

Answer: Use isna() or isnull(). To count missing values by column, use df.isna().sum().

Q12. What is the purpose of dropna()?

Answer: dropna() removes rows or columns containing missing values, depending on the parameters used.

Q13. What is the purpose of fillna()?

Answer: fillna() replaces missing values with specified values or other suitable replacement results.

Q14. How do you sort a DataFrame?

Answer: Use sort_values(), for example, df.sort_values(by="Marks", ascending=False).

Q15. How do you read a CSV file?

Answer: Use pd.read_csv("filename.csv").

Q16. How do you export a DataFrame to CSV?

Answer: Use df.to_csv("output.csv", index=False) when you do not want to export the index.

Q17. What does shape return?

Answer: The shape attribute returns a tuple containing the number of rows and columns for a DataFrame.

Q18. What is groupby() used for?

Answer: groupby() groups records according to one or more columns so that aggregations such as sums, means, counts, and maximums can be calculated.

Q19. What is the difference between concat() and merge()?

Answer: concat() combines objects along an axis, often by stacking rows. merge() combines DataFrames based on common key columns or indexes.

Q20. What is vectorization in Pandas?

Answer: Vectorization applies operations to entire Series or arrays, reducing the need for explicit Python loops and often improving performance.

36. Quick Revision Checklist

  • Understand the difference between Series and DataFrame.
  • Create Series using lists, dictionaries, and custom indexes.
  • Create DataFrames using dictionaries and lists of records.
  • Inspect data with head(), info(), describe(), and shape.
  • Select data using column names, loc, and iloc.
  • Filter records using Boolean conditions.
  • Add, modify, rename, and remove columns.
  • Sort data using sort_values().
  • Detect and handle missing values.
  • Convert data types and validate data quality.
  • Read and write CSV and Excel files.
  • Combine datasets using concat() and merge().
  • Group data using groupby() and calculate summary statistics.
  • Understand index alignment and vectorized calculations.

37. Conclusion

Pandas Series and DataFrames are essential tools for working with structured data in Python. A Series is useful for representing a single labeled variable, while a DataFrame organizes multiple variables into a tabular dataset. By learning how to create, inspect, select, filter, transform, clean, combine, and export these structures, learners can build a strong foundation for data analysis and preparation.

Practice these concepts with real datasets and gradually combine them into complete projects. For more information about Python training, visit the JustAcademy Python Training Course, explore the JustAcademy website, or register for a course demo.

whatsapp